﻿using AutoMapper;
using Compass.PurchaseService.Domain;
using Compass.PurchaseService.Domain.Entities;
using Compass.PurchaseService.Infrastructure;
using Compass.Wasm.Shared;
using Compass.Wasm.Shared.Purchases;
using Compass.Wasm.Shared.Wms;

namespace Compass.Wasm.Server.Services.Purchases;
public interface INavInvReportGroupService : IBaseService<NavInvReportGroupDto>
{
    Task<ApiResponse<NavInvReportGroupDto>> GetSingleByMonthAndGroupAsync(DateTime month,Group_e group);
    
    Task<ApiResponse<IEnumerable<NavInvReportGroupDto>>> GetAllWithMissGroupAsync();
}
public class NavInvReportGroupService : INavInvReportGroupService
{
    #region ctor
    private readonly PurchaseDbContext _context;
    private readonly PurchaseDbContext _dbContext;
    private readonly IPurchaseRepository _repository;
    private readonly IMapper _mapper;

    public NavInvReportGroupService(PurchaseDbContext dbContext, IPurchaseRepository repository, IMapper mapper)
    {
        _dbContext = dbContext;
        _repository = repository;
        _mapper = mapper;
    }
    #endregion

    #region 基本增删改查
    public async Task<ApiResponse<IEnumerable<NavInvReportGroupDto>>> GetAllAsync()
    {
        try
        {
            var models = await _repository.GetNavInvReportGroupsAsync();
            //先按照yearmonth排序，再按照group排序
            var dtos = await _mapper.ProjectTo<NavInvReportGroupDto>(models).OrderBy(x => x.YearMonth).ThenBy(x=>x.Group).ToListAsync();
            return new ApiResponse<IEnumerable<NavInvReportGroupDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<NavInvReportGroupDto>> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<NavInvReportGroupDto>> GetSingleAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetNavInvReportGroupByIdAsync(id);
            if (model == null) return new ApiResponse<NavInvReportGroupDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<NavInvReportGroupDto>(model);
            return new ApiResponse<NavInvReportGroupDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavInvReportGroupDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<NavInvReportGroupDto>> AddAsync(NavInvReportGroupDto dto)
    {
        try
        {
            var model = new NavInvReportGroup(Guid.NewGuid(), dto.YearMonth, dto.Group,dto.InvValue);
            await _dbContext.NavInvReportGroups.AddAsync(model);
            dto.Id = model.Id;
            return new ApiResponse<NavInvReportGroupDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavInvReportGroupDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<NavInvReportGroupDto>> UpdateAsync(Guid id, NavInvReportGroupDto dto)
    {
        try
        {
            var model = await _repository.GetNavInvReportGroupByIdAsync(id);
            if (model == null) return new ApiResponse<NavInvReportGroupDto> { Status = false, Message = "更新数据失败" };
            model.Update(dto);
            return new ApiResponse<NavInvReportGroupDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavInvReportGroupDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<NavInvReportGroupDto>> DeleteAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetNavInvReportGroupByIdAsync(id);
            if (model == null) return new ApiResponse<NavInvReportGroupDto> { Status = false, Message = "删除数据失败" };
            model.SoftDelete();//软删除
            return new ApiResponse<NavInvReportGroupDto> { Status = true };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavInvReportGroupDto> { Status = false, Message = e.Message };
        }
    }



    #endregion

    #region 扩展查询

    public async Task<ApiResponse<NavInvReportGroupDto>> GetSingleByMonthAndGroupAsync(DateTime month, Group_e group)
    {
        try
        {
            var model = await _repository.GetNavInvReportGroupByMonthAndGroupAsync(month,group);
            if (model == null) return new ApiResponse<NavInvReportGroupDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<NavInvReportGroupDto>(model);
            return new ApiResponse<NavInvReportGroupDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavInvReportGroupDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<IEnumerable<NavInvReportGroupDto>>> GetAllWithMissGroupAsync()
    {
        try
        {
            var models = await _repository.GetNavInvReportGroupsAsync();
            //先按照yearmonth排序，再按照group排序
            var orderDtos = await _mapper.ProjectTo<NavInvReportGroupDto>(models).OrderBy(x => x.YearMonth).ThenBy(x => x.Group).ToListAsync();
            //todo:按照YearMonth,Group的顺序查询，没有的赋值0
            var months = orderDtos.Select(x => x.YearMonth).Distinct();
            var groups = Enum.GetValues(typeof(Group_e)).Cast<Group_e>().Skip(1);
            var dtos=new List<NavInvReportGroupDto>();
            foreach (var month in months)
            {
                foreach (var group in groups)
                {
                    var dto = orderDtos.SingleOrDefault(x => x.YearMonth == month && x.Group == group);
                    if (dto != null)
                    {
                        dtos.Add(dto);
                    }
                    else
                    {
                        dtos.Add(new NavInvReportGroupDto()
                        {
                            YearMonth = month,
                            Group = group,
                            InvValue = 0
                        });
                    }
                }
            }

            return new ApiResponse<IEnumerable<NavInvReportGroupDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<NavInvReportGroupDto>> { Status = false, Message = e.Message };
        }
    }

    #endregion
}